#include "utilMenu.h"

#include <string.h>

bool isDigitOrDelimiter( char c) {
	return ((c >= '0') && (c <= '9')) || (c == BUFFER_DELIMITER);
}

static char buffer[22];
static char idx = 0;

char *getBuffer()
{
	return buffer;
}

void setBuffer( const char *_buffer)
{
	buffer[0] = BUFFER_DELIMITER;
	strcpy( buffer+1, _buffer);
	buffer[strlen(buffer+1)] = BUFFER_DELIMITER;
	idx = 0;
}

char getCurrentIndex()
{
	return idx;
}
void setCurrentIndex( char _idx)
{
	idx = _idx;
}

char getFirstDigit()
{
	idx = 0;
//	if( buffer[digit] != BUFFER_DELIMITER) {
		do {
			idx++;
		} while( !isDigitOrDelimiter( buffer[idx]));
//	}
	return buffer[idx];
}
char getNextDigit()
{
//	if( buffer[digit] != BUFFER_DELIMITER) {
		do {
			idx++;
		} while( !isDigitOrDelimiter( buffer[idx]));
//	}
	return buffer[idx];
}

char getPreviousDigit()
{
//	if( buffer[idx] != BUFFER_DELIMITER) {
		do {
			idx--;
		} while( !isDigitOrDelimiter( buffer[idx]));
//	}
	return buffer[idx];
}

void decDigit()
{
	buffer[idx]--;
	if( buffer[idx] < '0') {
		buffer[idx] = '9';
	}
}

void incDigit()
{
	buffer[idx]++;
	if( buffer[idx] > '9') {
		buffer[idx] = '0';
	}
}
